package com.air.ota.util;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class SZTInterface {
	
	public byte[] sendRequest(byte[] request) throws Exception{
		String host = ConfigProperties.getInstance().getValue("szt.host");
		int port=Integer.parseInt(ConfigProperties.getInstance().getValue("szt.port"));
		
		byte[] bufferResponse = new byte[1024] ;
		
		Socket client = new Socket(InetAddress.getByName(host), port);
		DataInputStream  in =new DataInputStream (client.getInputStream());
		DataOutputStream  out = new DataOutputStream(client.getOutputStream());
		
		
		out.write(request);
		in.read(bufferResponse);
		
		
		byte[] responseLenBytes = new byte[2];
		System.arraycopy(bufferResponse, 1, responseLenBytes, 0, 1);
		System.arraycopy(bufferResponse, 0, responseLenBytes, 1, 1);
		int responseLen= Integer.parseInt(HexStringUtil.bytesToHexString(responseLenBytes),16)+2;
		System.out.println("responseLen="+responseLen);
		byte[] response = new byte[responseLen];
		System.arraycopy(bufferResponse, 0, response, 0, responseLen);
		
		
		return response;
	}
	
}
